local audio = require("framework.audio")
local audioPath = require("app.res.audio.config")
local M = class("startDialog",function ( ... )
	return display.newLayer()
end)

function M:ctor(callback)
	self.m_callback = callback
	self.maskBg = display.newColorLayer(cc.c4b(0,0,0,180))
		:addTo(self)
	self.maskBg:setCascadeOpacityEnabled(true)
	self.bg = display.newSprite("#game_06.png")
				:addTo(self)
				:align(display.CENTER, display.cx+display.width, display.cy)
	local size = self.bg:getContentSize()
	local btn_start = cc.ui.UIPushButton.new{normal = "#game_16.png",pressed = "#game_14.png"}
				:addTo(self.bg)
				:align(display.CENTER_BOTTOM, size.width*0.5, 40)
				:onButtonPressed(function (  )
			        audio.playSound(audioPath.go)
			    end)
				:onButtonClicked(function ( ... )
					self:runOut()
				end)

	self:runIn()
end

function M:runIn()
	audio.playSound(audioPath.ready)
	local ani = transition.sequence{cc.MoveTo:create(0.5, cc.p(display.cx-60, display.cy)),cc.MoveTo:create(0.1, cc.p(display.cx, display.cy))}
	self.bg:runAction(ani)
	self.maskBg:runAction(transition.sequence({cc.FadeOut:create(0),cc.FadeIn:create(0.4)}))
end

function M:runOut()
	local seq = transition.sequence({cc.MoveTo:create(0.1, cc.p(display.cx-100, display.cy)),
							cc.CallFunc:create(function ( ... )
                                              	if self.m_callback then
	                                              	if self.m_callback and type(self.m_callback)=="function" then
														self.m_callback()
													end
                                              	end
                                          end),cc.MoveTo:create(0.5, cc.p(display.cx+display.width, display.cy)),
											cc.CallFunc:create(function ( ... )
												self:removeSelf()
                                          end)})
	self.bg:runAction(seq)

	self.maskBg:runAction(cc.FadeOut:create(0.4))
end

return M